<template>
  <div>
    <el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input>
    <el-tree
      :data="menus"
      @node-click="handleNodeClick"
      :expand-on-click-node="false"
      :props="defaultProps"
      node-key="catId"
      ref="menuTree"
      :filter-node-method="filterNode"
      :highlight-current = "true"
    >

    </el-tree>
  </div>
</template>
<script>

export default {
  name: 'MyTree',
  watch: {
    filterText (val) {
      this.$refs.menuTree.filter(val);
    }
  },
  data() {
    return {
      menus: [],
      category: {
        catId: null,
        name: '',
        parentCid: 0,
        showStatus: 1,
        sort: 0,
        catLevel: 0,
        icon: null,
        productUnit: null,
        productCount: null
      },
      defaultProps: {
        label: 'name',
        children: 'childList'
      },
      filterText: ''
    };
  },
  methods: {
    handleNodeClick (data, node, component) {
      console.log('子节点categoryTree被点击',data, node, component);
      //向父组件发送事件,包含子组件数据
      this.$emit("tree-node-click",data, node, component)

    },
    getMenus () {
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get',
      }).then((response) => {
        this.menus = response.data.data;
        console.log('response',this.menus);
      }).catch(() => {
        console.log("=========error==========")
        this.$message({
          type: 'error',
          message: '网络异常'
        });
      })
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    }
  },
  created() {
    this.getMenus();
  }
}
</script>
<style>

</style>


